package shared;

import java.nio.FloatBuffer;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL20;

public class Textwindow extends GUIBase 
{
	NinePatch m_patch;
	NuFont m_font;
	int m_TextureID,m_colourID;
	Vec2f m_pos,m_size;
	boolean m_active,m_uppercase;
	public String m_string;
	public String m_emptystring;
	public Textwindow (int []texid, Vec2f pos, Vec2f size,int colour, String emptystring)
	{
		m_uppercase=false;
		m_pos=pos;
		m_size=size;
		m_active=false;
		m_TextureID=texid[0];
		m_colourID=colour;
		m_string="";
		m_emptystring=emptystring;
		m_font=new NuFont(pos,texid[0],20,0.5F);
		m_patch=new NinePatch(pos, size.x, size.y, texid[1]);
		m_font.setString(m_emptystring);
	}
	
	@Override
	public void Update() {
		// TODO Auto-generated method stub
		if (m_active)
		{
			//if active, poll for character messages from the keyboard
			if (Keyboard.next())
			{
					
					boolean b=false;	
					//screen out the special keys 
					if (Keyboard.getEventKeyState())
					{		
						if (Keyboard.getEventKey()==Keyboard.KEY_BACK )
						{
							if (m_string.length()>0)
							{
								m_string=m_string.substring(0, m_string.length()-1);
							}
							b=true;
						}
						if (b==false)
						{
							char cha=Keyboard.getEventCharacter();
							m_string=m_string+cha;
	
						}
						Amend();	
					}			
			}
		}
		
	}
	
	void Amend()
	{
		if (m_string.length()>0)
		{
			m_font.setString(m_string);
		}
		else
		{
			m_font.setString(m_emptystring);
		}
	}
	@Override
	public boolean ClickEvent(Vec2f pos) {
		// TODO Auto-generated method stub
		//check 
		if (pos.x>m_pos.x && pos.x<m_pos.x+m_size.x)
		{
			if (pos.y>m_pos.y && pos.y<m_pos.y+m_size.y)
			{
				if (m_active==true)
				{
					m_active=false;
				}
				else
				{
					m_active=true;
				}
				return true;
			}
		}		
		return false;
	}
	@Override
	public void Draw(FloatBuffer buffer, int matrixloc) {
		// TODO Auto-generated method stub

		if (m_active==true)
		{
			GL20.glUniform4f(m_colourID,1.0F,1.0F,0.5F, 1);
			m_patch.Draw(buffer, matrixloc);	
			m_font.Draw(buffer, matrixloc);	
			GL20.glUniform4f(m_colourID,1.0F,1.0F,1.0F, 1);		
		}
		else
		{
			m_patch.Draw(buffer, matrixloc);
			m_font.Draw(buffer, matrixloc);			
		}

		
	}
	@Override
	public void Discard() {
		// TODO Auto-generated method stub
		m_font.Discard();
		m_patch.Discard();
	}
	@Override
	public void AdjustPos(Vec2f p) {
		// TODO Auto-generated method stub

		m_pos.x+=p.x;
		m_pos.y+=p.y;
		Vec2f pp=new Vec2f(m_pos.x+0.25F, m_pos.y+(m_size.y/2)-0.25F);	
		m_font.AdjustPos(pp);	
		m_patch.AdjustPos(m_pos);	
	}
	
	
	
	
}
